/*
 * @Author: MrDotYan 2564217790@qq.com
 * @Date: 2024-09-12 16:13:05
 * @LastEditors: MrDotYan 2564217790@qq.com
 * @LastEditTime: 2024-09-26 20:39:07
 * @FilePath: /webServerAdmin/src/hooks/useFile.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

const URL = import.meta.env.VITE_FILE_API;
export enum FileType {
  IMAGE,
  VIDEO,
  DOCUMENT,
  UNKNOWN
}

export function useFile(path?: string): string {
  if (path) {
    const urlIsHttp = path?.startsWith("http");
    if (urlIsHttp) return path;
    return `${URL}${path}`;
  }
  console.error("path is Not Empty");
  return "";
}

export function useFileType(mimeType: string): FileType {
  const imageTypes = ["image/jpeg", "image/png", "image/gif", "image/svg+xml", "image/webp"];
  const videoTypes = ["video/mp4", "video/x-msvideo", "video/x-matroska", "video/ogg"];
  const documentTypes = [
    "application/pdf",
    "application/msword",
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    "application/vnd.ms-excel",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  ];

  if (imageTypes.includes(mimeType)) return FileType.IMAGE;
  if (videoTypes.includes(mimeType)) return FileType.VIDEO;
  if (documentTypes.includes(mimeType)) return FileType.DOCUMENT;

  return FileType.UNKNOWN;
}
